// var CryptoJS = require("crypto-js");

export default {

    encrypt: function(jsonString, passphrase) {
        var salt = CryptoJS.lib.WordArray.random(256);
        var iv = CryptoJS.lib.WordArray.random(16);
        //for more random entropy can use : https://github.com/wwwtyro/cryptico/blob/master/random.js instead CryptoJS random() or another js PRNG

        var key = CryptoJS.PBKDF2(passphrase, salt, {
            hasher: CryptoJS.algo.SHA512,
            keySize: 64 / 8,
            iterations: 999
        });


        var encrypted = CryptoJS.AES.encrypt(jsonString, key, {
            iv: iv
        });

        var data = {
            ciphertext: CryptoJS.enc.Base64.stringify(encrypted.ciphertext),
            salt: CryptoJS.enc.Hex.stringify(salt),
            iv: CryptoJS.enc.Hex.stringify(iv)
        }

        return window.btoa(JSON.stringify(data));
    },
    decrypt: function(encrypted_base64encode_string, passphrase) {

        var obj_json = JSON.parse(window.atob(encrypted_base64encode_string));
        var encrypted = obj_json.ciphertext;
        var salt = CryptoJS.enc.Hex.parse(obj_json.salt);
        var iv = CryptoJS.enc.Hex.parse(obj_json.iv);

        var key = CryptoJS.PBKDF2(passphrase, salt, {
            hasher: CryptoJS.algo.SHA512,
            keySize: 64 / 8,
            iterations: 999
        });

        var decrypted = CryptoJS.AES.decrypt(encrypted, key, {
            iv: iv
        });

        return decrypted.toString(CryptoJS.enc.Utf8);

    }
};